#!/usr/bin/env python3
#
#  Uruk Update Manager
#  
#  Copyright 2022 hayder majid <hayder@riseup.net>
#  
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#  
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#  
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#  MA 02110-1301, USA.


from tkinter import *
from tkinter import messagebox
import os
import configparser

gui=Tk()
gui.configure(background='#ffffff')
gui.title("Uruk Update Manager")
photo = PhotoImage(file ='/usr/share/urukUpdater/pixmap/urukupdater-up-to-date.png')
gui.iconphoto(False, photo)
confDir = '/usr/share/urukUpdater/config.conf'
config = configparser.ConfigParser()
config.read(confDir)

try:
    time_ref = IntVar(value=int(config['CONFVAL']['time_refresh']))
    show_not = IntVar(value=int(config['CONFVAL']['show_notification']))
    # auto_ref = IntVar(value=int(config['CONFVAL']['auto_refresh']))
    full_upg = IntVar(value=int(config['CONFVAL']['full_upgrade']))
    print('Stored initial values loaded!')
except:
    time_ref = IntVar(value=int(config['DEFAULT']['time_refresh']))
    show_not = IntVar(value=int(config['DEFAULT']['show_notification']))
    # auto_ref = IntVar(value=int(config['DEFAULT']['auto_refresh']))
    full_upg = IntVar(value=int(config['DEFAULT']['full_upgrade']))
    print('Default values loaded!')
    
def apply_conf():
    try:
        int(textBox.get())
        config['CONFVAL']['time_refresh'] = str(int(textBox.get())*60)
        config['CONFVAL']['show_notification'] = str(show_not.get())
        # config['CONFVAL']['auto_refresh'] = str(auto_ref.get())
        config['CONFVAL']['full_upgrade'] = str(full_upg.get())

        if int(show_not.get()) == 1:
            os.system('sed -i "s/#NOTIFY8  /NOTIFY  /" /usr/lib/urukUpdater/setter')
        else:
            os.system('sed -i "s/NOTIFY  /#NOTIFY8   /" /usr/lib/urukUpdater/setter')
            
        # if int(auto_ref.get()) == 1:
            # os.system('touch /home/$USER/.urukUpdater/auto_ref.FLAG 2> /dev/null')
        # else:
            # os.system('rm /home/$USER/.urukUpdater/auto_ref.FLAG 2> /dev/null ') 
               
        if int(full_upg.get()) == 1:
            os.system('sed -i "s/NUM=.*/NUM=dist-upgrade/" /usr/lib/urukUpdater/u-mode')
            os.system('sudo /usr/lib/urukUpdater/u-mode')
        else:
            os.system('sed -i "s/NUM=.*/NUM=upgrade/" /usr/lib/urukUpdater/u-mode')
            os.system('sudo /usr/lib/urukUpdater/u-mode')   
        with open(confDir, 'w') as setconf:
            config.write(setconf)
        messagebox.showinfo(title="Apply changes", message="All changes have been applied ")   
        print("Stored ",textBox.get()," as new automatic update time, All changes have been applied " )	
        os.system('sudo /usr/lib/urukUpdater/checker &> /dev/null ')
    except ValueError:
        messagebox.showerror(title="error", message="Please enter a valid number")
        print('Wrong value entered!')   
	
def messageBox():
    messagebox.showinfo(title="About Uruk Update Manager", message="Uruk Update Manager \n Free and Open Source System Update Manager to manage your Debian based system \n by hayder majid <hayder@riseup.net> \n version 1.0 \n under (GPLv3) or later 2022 ")

labelList = Label(gui,text = "Preferences \n ",  fg = "#636e72", bg='#ffffff', font=("noto", 12, "bold"))
labelList.grid(column=1, row=1, padx=1, pady=3, sticky='w')

labelList = Label(gui,text = "Auto-refresh check time (in min)  ",  fg = "#636e72", bg='#ffffff', font=("noto", 9, "bold"))
labelList.grid(column=1, row=3, padx=1, pady=4, sticky='w')

labelList = Label(gui,text = "Show Desktop notification", fg = "#636e72", bg='#ffffff', font=("noto", 9, "bold"))
labelList.grid(column=1, row=5, padx=1, pady=4, sticky='w')

# labelList = Label(gui,text = "Automatically Download update ", fg = "#636e72", bg='#ffffff', font=( "noto", 9, "bold"))
# labelList.grid(column=1, row=7, padx=1, pady=4, sticky='w')

labelList = Label(gui,text = "Use Full Upgrade for \n checking and updating system", fg = "#636e72", bg='#ffffff', font=("noto", 9, "bold"))
labelList.grid(column=1, row=8, padx=1, pady=4, sticky='w')

textBox=Entry(gui, width=8, borderwidth=0, bg='#ffffff')
textBox.insert(0,int(time_ref.get()/60))
textBox.grid(column=0, row=3, padx=10, pady=14, sticky='e')

checkBox1 = Checkbutton(gui, variable = show_not, borderwidth=0,highlightthickness=0,bd=0, fg = "#636e72", bg='#ffffff')
checkBox1.grid(column=0, row=5, padx=10, pady=14, sticky='e')

# checkBox1 = Checkbutton(gui, variable = auto_ref, borderwidth=0, fg = "#636e72", bg='#ffffff')
# checkBox1.grid(column=0, row=7, padx=0, pady=4, sticky='e')

checkBox2 = Checkbutton(gui, variable = full_upg, borderwidth=0,highlightthickness=0,bd=0, fg = "#636e72", bg='#ffffff')
checkBox2.grid(column=0, row=8, padx=10, pady=14, sticky='e')

buttonDll=Button(gui, height=0, width=16, text="Apply", fg='white', bg='#5599ff', font=('noto', 10, "bold"), borderwidth=0, command=lambda: apply_conf())
buttonDll.grid(column=1, row=9,padx=10, pady=12)

buttonEx=Button(gui, height=0, width=8, text="Exit", fg='white', bg='#ff7675', font=('noto', 10, "bold"), borderwidth=0,  command=gui.destroy) 
buttonEx.grid(column=0, row=9, padx=10,pady=12)

gui.geometry("380x280")

mainloop()
